package com.ruoyi.mall.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.PmsAttributeMapper;
import com.ruoyi.mall.domain.PmsAttribute;
import com.ruoyi.mall.service.IPmsAttributeService;
import org.springframework.util.StringUtils;

import javax.xml.crypto.Data;

/**
 * 商品属性Service业务层处理
 *
 * @author zyy
 * @date 2023-09-17
 */
@Service
public class PmsAttributeServiceImpl implements IPmsAttributeService {
    @Autowired
    private PmsAttributeMapper pmsAttributeMapper;

    /**
     * 查询商品属性
     *
     * @param id 商品属性主键
     * @return 商品属性
     */
    @Override
    public PmsAttribute selectPmsAttributeById(Long id) {
        return pmsAttributeMapper.selectPmsAttributeById(id);
    }

    /**
     * 查询商品属性列表
     *
     * @param pmsAttribute 商品属性
     * @return 商品属性
     */
    @Override
    public List<PmsAttribute> selectPmsAttributeList(PmsAttribute pmsAttribute) {
        return pmsAttributeMapper.selectPmsAttributeList(pmsAttribute);
    }

    /**
     * 新增商品属性
     *
     * @param pmsAttribute 商品属性
     * @return 结果
     */
    @Override
    public int insertPmsAttribute(PmsAttribute pmsAttribute) {   //1.获取新增属性的名称
        String attributeName = pmsAttribute.getName();
        //2.判断属性名称是否为空
        if (StringUtils.isEmpty(attributeName)) {
            throw new RuntimeException("属性名称不能为空");
        }
        //3.判断新增的属性名称是否已存在
        int count = pmsAttributeMapper.selectCountByName(attributeName);
        if (count > 0) {
            throw new RuntimeException("新增的属性名称已存在");
        }
        Date date = new Date();
        pmsAttribute.setGmtCreate(date);
        pmsAttribute.setGmtModified(date);
        return pmsAttributeMapper.insertPmsAttribute(pmsAttribute);
    }

    /**
     * 修改商品属性
     *
     * @param pmsAttribute 商品属性
     * @return 结果
     */
    @Override
    public int updatePmsAttribute(PmsAttribute pmsAttribute) {
        //1.获取修改的属性名称
        String updateName = pmsAttribute.getName();
        //2.判断属性名称是否为空
        if (StringUtils.isEmpty(updateName)) {
            throw new RuntimeException("修改的属性名称不能为空");
        }
        //3.判断修改的属性名称是否重复
        PmsAttribute attribute = pmsAttributeMapper.selectAttributeByName(updateName);
        if (attribute != null) {
            if (attribute.getName().equals(updateName) && pmsAttribute.getId() != attribute.getId()) {
                throw new RuntimeException("该属性名称已存在,修改失败");
            }
        }
        pmsAttribute.setGmtModified(new Date());
        return pmsAttributeMapper.updatePmsAttribute(pmsAttribute);
    }

    /**
     * 批量删除商品属性
     *
     * @param ids 需要删除的商品属性主键
     * @return 结果
     */
    @Override
    public int deletePmsAttributeByIds(Long[] ids) {
        if (ids.length == 0) {
            throw new RuntimeException("请选中要删除的数据");
        }
        return pmsAttributeMapper.deletePmsAttributeByIds(ids);
    }

    /**
     * 删除商品属性信息
     *
     * @param id 商品属性主键
     * @return 结果
     */
    @Override
    public int deletePmsAttributeById(Long id) {
        return pmsAttributeMapper.deletePmsAttributeById(id);
    }
}
